<template>
  <div class="login-page">
    <!-- 动态背景 -->
    <div class="animated-bg">
      <div class="light-beam"></div>
      <div class="particles"></div>
      <div class="cyber-grid"></div>
    </div>

    <!-- 主内容区 -->
    <div class="login-container">
      <!-- Logo区域 -->
      <div class="logo-section">
        <div class="logo-wrapper">
          <svg width="100" height="100" viewBox="0 0 100 100">
            <defs>
              <linearGradient id="logoGradient" x1="0%" y1="0%" x2="100%" y2="100%">
                <stop offset="0%" style="stop-color:#4481eb"/>
                <stop offset="100%" style="stop-color:#04befe"/>
              </linearGradient>
            </defs>
            <circle cx="50" cy="50" r="48" fill="none" stroke="url(#logoGradient)" stroke-width="2">
              <animate attributeName="stroke-dasharray" from="0 300" to="300 300" dur="2s" fill="freeze"/>
            </circle>
            <text x="50" y="50" text-anchor="middle" dy=".3em" fill="url(#logoGradient)" 
                  font-size="40" font-weight="bold" opacity="0">
              E
              <animate attributeName="opacity" from="0" to="1" dur="1s" fill="freeze" begin="1s"/>
            </text>
          </svg>
        </div>
        <h1 class="brand-title">E特尔诺管理平台</h1>
        <p class="brand-subtitle">连接创新，驱动未来</p>
      </div>

      <!-- 表单区域 -->
      <div class="form-container glass-effect">
        <div class="form-tabs">
          <button 
            :class="['tab-btn', { active: activeTab === 'login' }]"
            @click="activeTab = 'login'"
          >
            <i class="fas fa-sign-in-alt"></i>
            登录
          </button>
          <button 
            :class="['tab-btn', { active: activeTab === 'register' }]"
            @click="activeTab = 'register'"
          >
            <i class="fas fa-user-plus"></i>
            注册
          </button>
          <div class="tab-indicator" :style="tabIndicatorStyle"></div>
        </div>

        <!-- 登录表单 -->
        <transition name="fade-slide" mode="out-in">
          <div v-if="activeTab === 'login'" class="form-content" key="login">
            <el-form ref="loginForm" :model="loginForm" :rules="loginRules">
              <div class="input-group">
                <el-input 
                  v-model="loginForm.account"
                  placeholder="账号"
                  @focus="handleInputFocus"
                  @blur="handleInputBlur"
                >
                  <template #prefix>
                    <div class="input-icon-wrapper">
                      <i class="fas fa-user"></i>
                    </div>
                  </template>
                </el-input>
              </div>

              <div class="input-group">
                <el-input
                  :type="showPassword ? 'text' : 'password'"
                  v-model="loginForm.password"
                  placeholder="密码"
                  @keyup.enter.native="submitForm('loginForm')"
                >
                  <template #prefix>
                    <div class="input-icon-wrapper">
                      <i class="fas fa-lock"></i>
                    </div>
                  </template>
                  <template #suffix>
                    <i 
                      :class="['fas', showPassword ? 'fa-eye-slash' : 'fa-eye']"
                      @click="togglePasswordVisibility"
                    ></i>
                  </template>
                </el-input>
              </div>

              <div class="form-options">
                <el-checkbox v-model="rememberMe">记住我</el-checkbox>
                <a href="#" class="forgot-password">忘记密码？</a>
              </div>

              <el-button 
                type="primary" 
                class="submit-btn"
                :loading="isSubmitting"
                @click="submitForm('loginForm')"
              >
                <span class="btn-content">
                  <i class="fas fa-arrow-right"></i>
                  <span>{{ isSubmitting ? '登录中...' : '登录' }}</span>
                </span>
              </el-button>
            </el-form>
          </div>

          <!-- 注册表单 -->
          <div v-else class="form-content" key="register">
            <el-form ref="registerForm" :model="registerForm" :rules="registerRules">
              <div class="input-group">
                <el-input 
                  v-model="registerForm.username"
                  placeholder="用户名"
                >
                  <template #prefix>
                    <div class="input-icon-wrapper">
                      <i class="fas fa-user"></i>
                    </div>
                  </template>
                </el-input>
              </div>

              <div class="input-group">
                <el-input 
                  v-model="registerForm.email"
                  placeholder="邮箱"
                >
                  <template #prefix>
                    <div class="input-icon-wrapper">
                      <i class="fas fa-envelope"></i>
                    </div>
                  </template>
                </el-input>
              </div>

              <div class="input-group">
                <el-input
                  :type="showPassword ? 'text' : 'password'"
                  v-model="registerForm.password"
                  placeholder="密码"
                >
                  <template #prefix>
                    <div class="input-icon-wrapper">
                      <i class="fas fa-lock"></i>
                    </div>
                  </template>
                  <template #suffix>
                    <i 
                      :class="['fas', showPassword ? 'fa-eye-slash' : 'fa-eye']"
                      @click="togglePasswordVisibility"
                    ></i>
                  </template>
                </el-input>
              </div>

              <div class="input-group">
                <el-input
                  :type="showPassword ? 'text' : 'password'"
                  v-model="registerForm.confirmPassword"
                  placeholder="确认密码"
                >
                  <template #prefix>
                    <div class="input-icon-wrapper">
                      <i class="fas fa-lock"></i>
                    </div>
                  </template>
                </el-input>
              </div>

              <el-button 
                type="primary" 
                class="submit-btn"
                :loading="isSubmitting"
                @click="submitForm('registerForm')"
              >
                <span class="btn-content">
                  <i class="fas fa-user-plus"></i>
                  <span>{{ isSubmitting ? '注册中...' : '注册' }}</span>
                </span>
              </el-button>
            </el-form>
          </div>
        </transition>
      </div>

      <!-- 版本信息 -->
      <div class="version-info">
        <span>Version 1.0.0</span>
        <div class="links">
          <a href="#" @click.prevent="showPrivacyPolicy">隐私政策</a>
          <span class="divider">|</span>
          <a href="#" @click.prevent="showTerms">服务条款</a>
        </div>
      </div>
    </div>

    <!-- 全局提示 -->
    <div class="toast" :class="{ 'show': showToast }">
      {{ toastMessage }}
    </div>
  </div>
</template>

<script>
import axios from 'axios';

export default {
  name: 'LoginPage',
  data() {
    // 密码确认验证
    const validatePass = (rule, value, callback) => {
      if (value !== this.registerForm.password) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };

    return {
      activeTab: 'login',
      showPassword: false,
      isSubmitting: false,
      rememberMe: false,
      
      // 登录表单
      loginForm: {
        account: '',
        password: ''
      },
      
      // 注册表单
      registerForm: {
        username: '',
        email: '',
        password: '',
        confirmPassword: ''
      },
      
      // 登录验证规则
      loginRules: {
        account: [
          { required: true, message: '请输入账号', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, message: '密码长度不能小于6位', trigger: 'blur' }
        ]
      },
      
      // 注册验证规则
      registerRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 2, message: '用户名长度不能小于2位', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入邮箱地址', trigger: 'blur' },
          { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, message: '密码长度不能小于6位', trigger: 'blur' }
        ],
        confirmPassword: [
          { required: true, message: '请再次输入密码', trigger: 'blur' },
          { validator: validatePass, trigger: 'blur' }
        ]
      },
      
      // 提示相关
      showToast: false,
      toastMessage: '',
      
      // 动画相关
      tabIndicatorStyle: {
        left: '0%',
        width: '50%'
      }
    };
  },
  
  methods: {
    // 表单提交
    async submitForm(formName) {
      try {
        await this.$refs[formName].validate();
        this.isSubmitting = true;
        
        const apiEndpoint = formName === 'loginForm' ? '/api/admin/login' : '/api/register';
        const params = formName === 'loginForm' ? {
          account: this.loginForm.account,
          password: this.loginForm.password
        } : {
          username: this.registerForm.username,
          email: this.registerForm.email,
          password: this.registerForm.password
        };

        const response = await axios.post(apiEndpoint, params);
        
        if (response.data.code === 20002) {
          localStorage.setItem('token', response.data.data);
          localStorage.setItem('account', response.data.currentMode);
          
          if (formName === 'loginForm') {
            this.$router.replace('/control-center');
            this.showToastMessage('登录成功');
          } else {
            this.showToastMessage('注册成功，请登录');
            this.activeTab = 'login';
          }
        } else {
          throw new Error(response.data.message);
        }
      } catch (error) {
        this.showToastMessage(error.message || (formName === 'loginForm' ? '登录失败' : '注册失败'));
      } finally {
        this.isSubmitting = false;
      }
    },

    // 切换密码可见性
    togglePasswordVisibility() {
      this.showPassword = !this.showPassword;
    },

    // 显示提示消息
    showToastMessage(message) {
      this.toastMessage = message;
      this.showToast = true;
      setTimeout(() => {
        this.showToast = false;
      }, 3000);
    },

    // 处理输入框焦点
    handleInputFocus(e) {
      e.target.parentElement.classList.add('input-focused');
    },

    handleInputBlur(e) {
      e.target.parentElement.classList.remove('input-focused');
    },

    // 切换标签页
    switchTab(tab) {
      this.activeTab = tab;
      this.tabIndicatorStyle.left = tab === 'login' ? '0%' : '50%';
    },

    // 显示隐私政策
    showPrivacyPolicy() {
      this.$router.push('/privacy-policy');
    },

    // 显示服务条款
    showTerms() {
      this.$router.push('/terms');
    }
  },
  
  mounted() {
    document.title = 'E特尔诺-登录';
  }
};
</script>

<style scoped>
/* 基础样式 */
.login-page {
  min-height: 100vh;
  background: #f0f2f5;
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
  overflow: hidden;
}

/* 动态背景 */
.animated-bg {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  overflow: hidden;
}

.light-beam {
  position: absolute;
  width: 200%;
  height: 200%;
  top: -50%;
  left: -50%;
  background: linear-gradient(
    45deg,
    rgba(66, 133, 244, 0.1) 0%,
    rgba(66, 133, 244, 0.05) 100%
  );
  animation: rotate 20s linear infinite;
}

.particles {
  position: absolute;
  width: 100%;
  height: 100%;
  background-image: radial-gradient(circle at center, #4285f4 1px, transparent 1px);
  background-size: 50px 50px;
  animation: float 20s linear infinite;
}

.cyber-grid {
  position: absolute;
  width: 200%;
  height: 200%;
  top: -50%;
  left: -50%;
  background-image: 
    linear-gradient(rgba(66, 133, 244, 0.05) 1px, transparent 1px),
    linear-gradient(90deg, rgba(66, 133, 244, 0.05) 1px, transparent 1px);
  background-size: 30px 30px;
  animation: grid-move 20s linear infinite;
}

/* 主容器 */
.login-container {
  width: 100%;
  max-width: 460px;
  padding: 2rem;
  position: relative;
  z-index: 1;
}

/* Logo区域 */
.logo-section {
  text-align: center;
  margin-bottom: 2rem;
}

.logo-wrapper {
  display: inline-block;
  margin-bottom: 1rem;
  animation: float 6s ease-in-out infinite;
}

.brand-title {
  font-size: 2rem;
  font-weight: 700;
  color: #1a237e;
  margin-bottom: 0.5rem;
}

.brand-subtitle {
  font-size: 1rem;
  color: #666;
  opacity: 0.8;
}

/* 表单容器 */
.form-container {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  border-radius: 16px;
  padding: 2rem;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
}

/* 表单标签页 */
.form-tabs {
  display: flex;
  margin-bottom: 2rem;
  position: relative;
  border-bottom: 2px solid #eee;
}

.tab-btn {
  flex: 1;
  padding: 1rem;
  border: none;
  background: none;
  color: #666;
  font-size: 1rem;
  cursor: pointer;
  transition: all 0.3s ease;
  position: relative;
  z-index: 1;
}

.tab-btn.active {
  color: #1a237e;
  font-weight: 600;
}

.tab-indicator {
  position: absolute;
  bottom: -2px;
  height: 2px;
  background: #1a237e;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

/* 输入框样式优化 */
.input-group {
  position: relative;
  margin-bottom: 1.5rem;
}

.input-group .el-input input {
  height: 50px;
  padding: 0 45px; /* 调整左右内边距，为图标留出空间 */
  border-radius: 10px;
  border: 2px solid #eee;
  font-size: 1rem;
  transition: all 0.3s ease;
  background: rgba(255, 255, 255, 0.8);
}

.input-group .el-input input:focus {
  border-color: #1a237e;
  box-shadow: 0 0 0 3px rgba(26, 35, 126, 0.1);
}

/* 左侧图标样式 */
.input-icon-wrapper {
  position: absolute;
  left: 15px; /* 调整左侧图标位置 */
  top: 50%;
  transform: translateY(-50%);
  color: #666;
  z-index: 2;
}

/* 右侧图标样式 */
.el-input__suffix {
  right: 15px; /* 调整右侧图标位置 */
  height: 100%;
  display: flex;
  align-items: center;
}

.el-input__suffix i {
  font-size: 1.1rem;
  color: #666;
  cursor: pointer;
  padding: 0 5px;
}

.el-input__suffix i:hover {
  color: #1a237e;
}

/* 输入框焦点状态下的图标颜色 */
.input-focused .input-icon-wrapper,
.el-input.is-focus .el-input__suffix i {
  color: #1a237e;
}

/* 暗色主题适配 */
.dark-theme .input-group .el-input input {
  background: rgba(45, 45, 45, 0.8);
  border-color: #444;
  color: #fff;
}

.dark-theme .input-icon-wrapper,
.dark-theme .el-input__suffix i {
  color: #aaa;
}

.dark-theme .input-focused .input-icon-wrapper,
.dark-theme .el-input.is-focus .el-input__suffix i {
  color: #64b5f6;
}

/* 按钮样式 */
.submit-btn {
  width: 100%;
  height: 48px;
  background: linear-gradient(45deg, #1a237e, #3949ab);
  border: none;
  border-radius: 8px;
  color: white;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  position: relative;
  overflow: hidden;
}

.submit-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(26, 35, 126, 0.2);
}

.submit-btn:active {
  transform: translateY(0);
}

.btn-content {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
}

/* 选项样式 */
.form-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin: 1rem 0 2rem;
}

.forgot-password {
  color: #1a237e;
  text-decoration: none;
  font-size: 0.9rem;
  transition: all 0.3s ease;
}

.forgot-password:hover {
  text-decoration: underline;
}

/* 版本信息 */
.version-info {
  text-align: center;
  margin-top: 2rem;
  color: #666;
  font-size: 0.9rem;
}

.links {
  margin-top: 0.5rem;
}

.links a {
  color: #1a237e;
  text-decoration: none;
  margin: 0 0.5rem;
  transition: all 0.3s ease;
}

.links a:hover {
  text-decoration: underline;
}

.divider {
  color: #ddd;
}

/* Toast提示 */
.toast {
  position: fixed;
  bottom: 2rem;
  left: 50%;
  transform: translateX(-50%) translateY(100px);
  background: rgba(0, 0, 0, 0.8);
  color: white;
  padding: 1rem 2rem;
  border-radius: 30px;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  opacity: 0;
  visibility: hidden;
}

.toast.show {
  transform: translateX(-50%) translateY(0);
  opacity: 1;
  visibility: visible;
}

/* 动画效果 */
@keyframes rotate {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}

@keyframes float {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-20px); }
}

@keyframes grid-move {
  from { transform: translateY(0); }
  to { transform: translateY(30px); }
}

/* 响应式设计 */
@media (max-width: 480px) {
  .login-container {
    padding: 1rem;
  }
  
  .form-container {
    padding: 1.5rem;
  }
  
  .brand-title {
    font-size: 1.5rem;
  }
  
  .form-options {
    flex-direction: column;
    gap: 1rem;
    align-items: flex-start;
  }
}

/* 暗色主题支持 */
@media (prefers-color-scheme: dark) {
  .login-page {
    background: #1a1a1a;
  }
  
  .form-container {
    background: rgba(30, 30, 30, 0.95);
  }
  
  .brand-title {
    color: #fff;
  }
  
  .brand-subtitle {
    color: #aaa;
  }
  
  .tab-btn {
    color: #aaa;
  }
  
  .tab-btn.active {
    color: #64b5f6;
  }
  
  .tab-indicator {
    background: #64b5f6;
  }
  
  .input-group .el-input input {
    background: #2d2d2d;
    border-color: #444;
    color: #fff;
  }
  
  .input-group .el-input input:focus {
    border-color: #64b5f6;
    box-shadow: 0 0 0 3px rgba(100, 181, 246, 0.1);
  }
  
  .submit-btn {
    background: linear-gradient(45deg, #64b5f6, #2196f3);
  }
  
  .forgot-password,
  .links a {
    color: #64b5f6;
  }
}
</style>
